package DeleteRole

import (
	codecs "sanguo/codec/cs"
	"sanguo/node/common/asynwrap"
	"sanguo/node/common/db"
	"sanguo/node/common/transaction"
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role"
	"sanguo/node/node_game/user"
	cs_message "sanguo/protocol/cs/message"
	"strconv"
	"time"

	"github.com/go-redis/redis"
	"github.com/golang/protobuf/proto"
)

type transactionDeleteRole struct {
	transaction.TransactionBase
	user    *user.User
	req     *codecs.Message
	resp    *cs_message.DeleteRoleToC
	errcode cs_message.EnumType
}

func (this *transactionDeleteRole) GetModuleName() string {
	return "DeleteRole"
}

func (this *transactionDeleteRole) GetReplyMsg() proto.Message {
	return &cs_message.DeleteRoleToC{}
}

func (this *transactionDeleteRole) SetExpiredTime(expired time.Time) {
	this.Expired = expired
}

func (this *transactionDeleteRole) GetExpiredTime() time.Time {
	return this.Expired
}

func (this *transactionDeleteRole) Begin() {
	this.resp = &cs_message.DeleteRoleToC{}
	msg := this.req.GetData().(*cs_message.DeleteRoleToS)
	roleModule := this.user.GetModuleByName("role").(*role.RoleModule)
	r := roleModule.GetRole(msg.GetID())
	if r == nil {
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}
	node_game.Infoln(r.GetUserID(), "Call DeleteRole :", msg.GetID())
	roleName := r.GetName()

	key := "role:" + this.user.GetUserID()
	asynwrap.Wrap(db.GetRedisClient("game").HDel)(func(ret []interface{}) {
		if !this.user.CheckTrans(this) {
			return
		}
		_, err := ret[0].(*redis.IntCmd).Result()

		if err != nil {
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			node_game.Infoln("delRole err:", err)
		} else {
			roleModule.DelRole(msg.GetID())
			this.HDelRoleModuleData(msg.GetID())

			this.HDelRoleName(roleName)
		}
	}, key, strconv.FormatUint(msg.GetID(), 10))

}

func (this *transactionDeleteRole) HDelRoleModuleData(roleId uint64) {

	key := "roleModuleData:" + strconv.FormatUint(roleId, 10)

	asynwrap.Wrap(db.GetRedisClient("game").Del)(func(ret []interface{}) {
		if !this.user.CheckTrans(this) {
			return
		}
		_, err := ret[0].(*redis.IntCmd).Result()

		if err != nil {
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			node_game.Infoln("delete  RoleModuleData fail:", err)
		} else {
			node_game.Infoln("delete  RoleModuleData ok")
			this.errcode = cs_message.EnumType_OK
			this.user.EndTrans(this)

		}
	}, key)
}

func (this *transactionDeleteRole) HDelRoleName(roleName string) {
	//同时删除名字
	asynwrap.Wrap(db.GetRedisClient("game").HDel)(func(ret []interface{}) {
		_, err := ret[0].(*redis.IntCmd).Result()
		if err == nil {
			node_game.Infoln("DelRole ok and Delete rolename ok ")
		} else {
			node_game.Infoln("DelRole ok and Delete rolename fail:", err)
		}
	}, "role_name_map", roleName)
}
func (this *transactionDeleteRole) End() {
	this.resp.Errcode = cs_message.EnumType(this.errcode).Enum()
	this.user.Reply(this.req.GetSeriNo(), this.resp)
}

func (this *transactionDeleteRole) Timeout() {
	this.errcode = cs_message.EnumType_RETRY
	this.user.EndTrans(this)
}

func init() {
	user.RegisterTransFunc(&cs_message.DeleteRoleToS{}, func(user *user.User, msg *codecs.Message) transaction.Transaction {
		return &transactionDeleteRole{
			user: user,
			req:  msg,
		}
	})
}
